package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    //查询商品，get提交，五个参数，返回查询品牌的列表。请求路径。item/brand/list?pid=0
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(@RequestParam(value = "key", required = false) String key,
                                                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                                                              @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                              @RequestParam(value = "sortBy", required = false) String sortBy,
                                                              @RequestParam(value = "desc", defaultValue = "false") Boolean desc) {


        PageResult<Brand> pageResult = brandService.queryBrandByPage(key, page, rows, sortBy, desc);
        if (pageResult == null || pageResult.getItems().size() == 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(pageResult);
    }

    //新增商品，post提交，没有返回值，参数为brand和cids分类数组。请求路径item/brand
    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.saveBrand(brand, cids);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    //删除商品
    @DeleteMapping("delete")
    public ResponseEntity<Void> deleteBrand(@RequestParam("bid") long bid) {

        this.brandService.deleteBrand(bid);
        return ResponseEntity.status(HttpStatus.OK).build();

    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids) {
        this.brandService.updateBrand(brand, cids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    //根据分类的id查询商品的名字
    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCategory(@PathVariable("cid") Long cid) {
        List<Brand> brands = this.brandService.queryBrandByCategory(cid);
        if (brands != null && brands.size() != 0) {
            return ResponseEntity.ok(brands);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }
    //根据商品的id查询商品的名字
    @GetMapping("bid/{bid}")
    public ResponseEntity<String> queryBrandByBrandId(@PathVariable("bid") Long bid) {
        String name = this.brandService.queryBrandNameById(bid);
        if (name != null ) {
            return ResponseEntity.ok(name);
        }
        return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    //根据商品的ids查询商品的列表
    @GetMapping("/brands")
   public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("ids") List<Long> bids){
       List<Brand> brands= this.brandService.queryBrandByIds(bids);
       if (brands==null){
           return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
       }
       return ResponseEntity.ok(brands);

    }
}
